import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;


/**
 * @author LKQ
 * @date 2022/3/6 14:46
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {6, 4, 3, 2, 7, 6, 2};
        solution.replaceNonCoprimes(nums);
    }
    public List<Integer> replaceNonCoprimes(int[] nums) {
        Stack<Integer> s = new Stack<>();
        s.add(nums[0]);
        int n = nums.length;
        for (int i = 1; i < n; i++) {
            s.add(nums[i]);
            while (s.size() > 1) {
                int x = s.get(s.size() - 2);
                int y = s.get(s.size() - 1);
                int g = gcd(x,y);
                if (g == 1) {
                    break;
                }
                s.remove(s.size() - 1);
                s.set(s.size() - 1, x / g * y);
            }
        }
        return s;
    }

    private int gcd(int x,  int y) {
        return x == 0 ? y : gcd(y % x, x);
    }

}
